package com.sn.author.controller;

import com.alibaba.fastjson.JSON;
import com.sn.author.common.annotation.Advice;
import com.sn.author.common.annotation.IgnoreToken;
import com.sn.author.common.context.SessionHelper;
import com.sn.author.domain.BaseResult;
import com.sn.author.domain.vo.AuthorCodeVo;
import com.sn.author.domain.vo.CallbackVo;
import com.sn.author.domain.vo.HeartVo;
import com.sn.author.common.enums.ExceptionEnum;
import com.sn.author.filter.content.SecurityContextHolder;
import com.sn.author.service.dao.ApiService;
import com.sn.author.service.impl.ApiServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author xiaom
 * @Date 2020/9/9 14:33
 * @Version 1.0.0
 * @Description <>
 **/
@RestController
@RequestMapping(value = "/api")
@Slf4j
public class Api {

    private final ApiService apiService;

    public Api(ApiService apiService) {
        this.apiService = apiService;
    }

    @PostMapping(value = "/authorCode")
    @IgnoreToken
    public BaseResult authorCode(@RequestBody AuthorCodeVo authorCodeVo) throws Exception {
        log.info("Params:{}", JSON.toJSONString(authorCodeVo));
        return BaseResult.success(apiService.authorCode(authorCodeVo.getKey()));
    }

    @PostMapping(value = "/heart")
    @IgnoreToken
    public BaseResult heart(@RequestBody HeartVo heartVo){
        if(heartVo.getSing() == null){
            return BaseResult.makeExceptionResult(ExceptionEnum.PARAMS_MISS_ERR.getMessage());
        }
        log.info("Do heart. Ip:[{}] sing:[{}] time:[{}]", SessionHelper.getContextRequest().getRemoteAddr(),heartVo.getSing(),heartVo.getTime());
        return BaseResult.success(apiService.heart(heartVo));
    }

    @GetMapping("test")
    public BaseResult test(){
        return BaseResult.success(SecurityContextHolder.getCurrentUserName());
    }

    @IgnoreToken
    @PostMapping(value = "/webHookMessage")
    @ApiOperation("回调消息 (通用)")
    public BaseResult<Boolean> webHookMessage(@RequestBody @Advice(need = false) CallbackVo callbackVo){
        return BaseResult.success(apiService.callback(callbackVo));
    }
}
